package ciyuan.Item.SkillBook;

import ciyuan.Effect.Effects;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.Passive.MagicIncrease;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;

/**
 * @author 150149
 */
public class ThunderPossessed extends SkillBookType {

    public ThunderPossessed() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 45 * 1000);

        if (playerEntity.level.isClientSide) {
            return;
        }
        playerEntity.addEffect(new EffectInstance(Effects.thunder_possessed, 300, 0));

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    public static void onAttack(World world, Entity target,PlayerEntity playerEntity) {
        if (world instanceof ServerWorld) {
            if (playerEntity.hasEffect(Effects.thunder_possessed)) {
                ServerWorld serverWorld = (ServerWorld) world;
                LightningBoltEntity lightningboltentity = EntityType.LIGHTNING_BOLT.create(serverWorld);
                lightningboltentity.moveTo(Vector3d.atBottomCenterOf(target.blockPosition()));
                lightningboltentity.setVisualOnly(true);
                serverWorld.addFreshEntity(lightningboltentity);
                target.hurt(DamageSource.playerAttack(playerEntity),10);
                MagicIncrease.onSkillHit(playerEntity);
                target.invulnerableTime = 0;
            }
        }
    }

    @Override
    public String getSkillCastName() {
        return "ThunderPossessed";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.4");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.4");
    }
}
